package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.entity.OrderReturnApplyEntity;
import com.rabbitmq.client.Channel;
import com.sun.org.apache.xalan.internal.xsltc.dom.SortingIterator;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;


@Service("orderItemService")
@RabbitListener(queues = {"hello-java-queue"})
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 原生消息详细信息
     *
     * @param
     */

    @RabbitHandler
    public void receiveMessage(Message message, OrderReturnApplyEntity content, Channel channel){

        byte[] body = message.getBody();
        MessageProperties properties = message.getMessageProperties();
        System.out.println("接收到消息。。" + message + "==>内容" +content);
        // channel里面消息的标签，就是一种顺序吧 1 2 3 4 5，重新入队标签是会变化的
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {

            if (deliveryTag %2 == 0) {
                // 非批量签收 long deliveryTag, boolean multiple
                channel.basicAck(deliveryTag,false);
            }else {
                //拒绝签收非批量处理、不重新入队,long deliveryTag, boolean multiple, boolean requeue
                channel.basicNack(deliveryTag,false,false);
            }

        } catch (IOException e) {
            // 网络中断了
            e.printStackTrace();
        }
    }


}